package net.zabuchy.issue.bo;
 
import java.util.Date;
import java.util.List;
 
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
 
import net.zabuchy.IssueHelper.IssueStatuses;
import net.zabuchy.helper.SessionHelper;
import net.zabuchy.issue.dao.IssueDao;
import net.zabuchy.issue.entity.Attachment;
import net.zabuchy.issue.entity.Issue;
import net.zabuchy.issue.entity.IssueLink;
import net.zabuchy.issue.entity.Notification;
import net.zabuchy.iteration.bo.IterationBo;
import net.zabuchy.iteration.entity.Iteration;
import net.zabuchy.project.bo.ProjectBo;
 
@Transactional
public class IssueBoImpl implements IssueBo {
 
    private static final Logger LOGGER = Logger.getLogger(IssueBoImpl.class.getName());
 
    @Autowired
    private SessionHelper sessionBean;
    
    @Autowired
    private IssueDao issueDao;
    
    @Autowired
    private ProjectBo projectBo;
    
    @Autowired
    private IterationBo iterationBo;
    
    
    public IssueDao getIssueDao() {
		return issueDao;
	}

	public void setIssueDao(IssueDao issueDao) {
		this.issueDao = issueDao;
	}

	public void add(Issue issue) {
		LOGGER.debug("add");
        if (issue == null) {
            return;
        }
        
        issue.setAuthor(sessionBean.getExecutor().getUsername());
        issue.setCreationDate(new Date());
        issue.setCustomerId(sessionBean.getCustomerId());
        issue.setProjectId(sessionBean.getProjectId());
        Iteration iteration = iterationBo.findById(1);
        
        LOGGER.debug("dupa");
        issue.setIteration(iteration);
        
        issueDao.add(issue);
        LOGGER.debug("Adding the following issue: " + issue.getIssueId());
    }
	
	public void addAttachment(Attachment attach) {
		issueDao.addAttachment(attach);
	}
 
	public void addLink(IssueLink link) {
		issueDao.addLink(link);
	}
	
	public void addNotification(Notification notification) {
		issueDao.addNotification(notification);
	}
	
    public void delete(Issue issue) {
        if (issue == null) {
            return;
        }
        LOGGER.debug("Deleteting the following issue: " + issue.getIssueId());
        issueDao.delete(issue);
    }
 
    public void deleteNotification(Notification noti) {
    	if (noti == null) {
            return;
        }
        LOGGER.debug("Deleteting the following noti: " + noti.getNotificationId());
        issueDao.deleteNotification(noti);
    }
    
    public void deleteAttachment(Attachment attach) {
    	if (attach == null) {
            return;
        }
        LOGGER.debug("Deleteting the following attach: " + attach.getAttachmentId());
        issueDao.deleteAttachment(attach);
    }
    
    public void deleteLink(IssueLink link) {
    	if (link == null) {
            return;
        }
        LOGGER.debug("Deleteting the following link: " + link.getLink());
        issueDao.deleteLink(link);
    }
    
    public List<Issue> findAll() {
        LOGGER.debug("Getting all issues");
        return issueDao.findAll();
    }
 
    public Issue findById(int id) {
    	if(id <= 0) {
    		LOGGER.debug("error: id <= 0");
    		return null;
    	}
        return issueDao.findById(id);
    }
 
 
    public void update(Issue issue) {
        if (issue == null) {
            return;
        }
        issueDao.update(issue);
    }
    
    public List<Issue> findIssuesRelatedToIteration(Integer id) {
    	if(id <= 0) {
    		LOGGER.debug("error: id <= 0");
    		return null;
    	}
    	return issueDao.findIssuesRelatedToIteration(id);
    }
    
    public List<Issue> findAllForProject(int projectId) {
    	return issueDao.findAllForProject(projectId);
    }
    
    public List<Issue> findAllForProjectAndIteration(int projectId, int iterationId) {
    	return issueDao.findAllForProjectAndIteration(projectId, iterationId);
    }
    
    public List<Issue> findAllForProjectAndIterationAndStatus(int projectId, int iterationId, IssueStatuses status) {
    	return issueDao.findAllForProjectAndIterationAndStatus(projectId, iterationId, status);
    }
    
    public List<Issue> findAllForProjectToConfirmation(int projectId, int iterationId) {
    	return issueDao.findAllForProjectToConfirmation(projectId, iterationId);
    }
    
	/**
	 * @param sessionBean the sessionBean to set
	 */
	public void setSessionBean(SessionHelper sessionBean) {
		this.sessionBean = sessionBean;
	}

	/**
	 * @return the sessionBean
	 */
	public SessionHelper getSessionBean() {
		return sessionBean;
	}

	public ProjectBo getProjectBo() {
		return projectBo;
	}

	public void setProjectBo(ProjectBo projectBo) {
		this.projectBo = projectBo;
	}

	/**
	 * @param iterationBo the iterationBo to set
	 */
	public void setIterationBo(IterationBo iterationBo) {
		this.iterationBo = iterationBo;
	}

	/**
	 * @return the iterationBo
	 */
	public IterationBo getIterationBo() {
		return iterationBo;
	}
    
    
}